package com.dwxt.cms.service;

import com.dwxt.cms.dao.UserRoleMapper;
import com.dwxt.cms.entity.UserRole;
import com.dwxt.cms.entity.UserRoleExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserRoleServiceAdmin {
    @Autowired
    private UserRoleMapper userRoleMapper;

    public List<UserRole> selectByUserId(Integer id) {
        UserRoleExample example = new UserRoleExample();
        example.createCriteria().andUserIdEqualTo(id);
        return userRoleMapper.selectByExample(example);

    }

    public List<UserRole> selectByRoleId(Integer id) {
        UserRoleExample example = new UserRoleExample();
        example.createCriteria().andRoleIdEqualTo(id);
        return userRoleMapper.selectByExample(example);
    }

    public void batchInsert(Integer userId, List<Integer> id) {
        List<UserRole> userRoles = id.stream().map(roleId -> {
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(roleId);
            return userRole;
        }).collect(Collectors.toList());
        userRoleMapper.batchInsert(userRoles);
    }

    public void deleteByUserIds(Integer[] id) {
        UserRoleExample userRoleExample = new UserRoleExample();
        userRoleExample.createCriteria().andUserIdIn(Arrays.asList(id));
        userRoleMapper.deleteByExample(userRoleExample);
    }

    public void deleteByUserId(Integer id) {
        UserRoleExample example = new UserRoleExample();
        example.createCriteria().andUserIdEqualTo(id);
        userRoleMapper.deleteByExample(example);
    }
}

